package com.javasm.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.sys.*;
import com.javasm.sys.tree.TreeUtil;
import com.javasm.commons.util.UserKey;
import com.javasm.service.RedisService;
import com.javasm.sys.dao.RoleDao;
import com.javasm.sys.dao.UserDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private RoleDao roleDao;
    @Resource
    private RoleService roleService;

    @Resource
    private RedisService redisService;

    @Resource
    private MenuService menuService;

    @Override
    public List<String> listPremissionsByRoleId(String roleId) {
        return roleService.getMenuPermits(roleId);
    }

    @Override
    public List<SysUser> getUserByDeptAndPosition(String deptId, String posId) {
        List<SysUser> l = userDao.getUsersBydeptidAndpid(deptId,posId);
        return l;
    }

    @Override
    public SysUser getOneByUname(String uname){
        return userDao.getOneByUserAccount(uname);
    }

    @Override
    public SysUser getUserByAccount(SysUser user) {
        SysUser userByAccount = userDao.getUserByAccount(user);
        if (userByAccount != null) {
            redisService.set(UserKey.LOGIN_KEY+user.getUserAccount(),userByAccount,1, TimeUnit.HOURS);
        }else {
            redisService.set(UserKey.LOGIN_KEY+user.getUserAccount(),UserKey.NOT_EXIST_USER,30,TimeUnit.SECONDS);
        }
        return userByAccount;
    }

    @Override
    public List<SysDepart> buildDeptTree() {

        //查询所有部门信息
        List<SysDepart> allDept = userDao.getAllDept();
        return buildDeptByPid(allDept, "-1");
    }

    //    按条件查询用户数据
    @Override
    public List<SysUser> getUsers(SysUser user) {
        List<SysUser> users = userDao.getUsers(user);
        return users;
    }

    @Override
    public Integer updateUserById(SysUser user) {
        return userDao.updateUserById(user);
    }

    @Override
    public Integer addUser(SysUser user) {
        return userDao.addUser(user);
    }

    @Override
    public SysUser getParms(String userId) {
        return userDao.getParms(userId);
    }

    @Override
    public Integer delUserById(String userId) {
        return userDao.delUserById(userId);
    }

    @Override
    public Integer resetPwd(String userId) {
        return userDao.resetPwd(userId);
    }

    @Override
    public Integer delUserByIds(List<String> ids) {
        return userDao.delUserByIds(ids);
    }

    @Override
    public List<SysMenu> getMenus(String userId) {
        SysUser user = userDao.getUserById(userId);
        Integer roleId = user.getRoleId();
        List<Integer> menuIds = roleDao.getMenuList(String.valueOf(roleId));
        List<SysMenu> menus = menuService.getMenusbyIds(menuIds);
        menus=menus.stream().filter(a->a.getMenuType()!=2).collect(Collectors.toList());
        List<SysMenu> menusTree = TreeUtil.buildMenuTree(menus, 0);
        return menusTree;
    }

    @Override
    public SysUser getUserById(String userId) {
        return userDao.getUserById(userId);
    }

    //根据pid构建tree树
    private List<SysDepart> buildDeptByPid(List<SysDepart> allDept, String pid) {
        List<SysDepart> r = new ArrayList<>();
        for (SysDepart dept : allDept) {
            if (pid.equals(dept.getPid())) {
                String id = dept.getDepartId();
                List<SysDepart> childs = buildDeptByPid(allDept, id);
                if (childs != null && childs.size() > 0) {
                    dept.setDepartList(childs);
                }
                r.add(dept);
            }
        }
        return r;
    }
}
